package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-07
 * Time：18:09
 */
@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        BlogDao blogDao = new BlogDao();
        String blogId = req.getParameter("blogId");
        String jsonStr = null;
        if (blogId!=null) {
            //存在blogId参数，返回博客
            Blog blog = blogDao.selectOne(Integer.parseInt(blogId));
            jsonStr = objectMapper.writeValueAsString(blog);
        } else {
            //不存在blogId参数，返回列表
            List<Blog> blogs = blogDao.selectAll();
            jsonStr = objectMapper.writeValueAsString(blogs);
        }
        resp.getWriter().write(jsonStr);
        resp.getWriter().flush();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.检查登陆状态
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("用户未登陆，请登录！！");
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("用户未登陆，请登录！！");
            return;
        }
        //2.读取请求内容
        req.setCharacterEncoding("utf8");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if (title == null || content == null || title.isEmpty() || content.isEmpty()) {
            resp.setStatus(400);
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("正文或内容不完整");
            return;
        }
        //3.构造blog写入数据库
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserId());
        BlogDao blogDao = new BlogDao();
        blogDao.insert(blog);
        //4.更新数据库数据，并且更新数据库表
        UserDao userDao = new UserDao();
        User temp = userDao.selectById(blog.getUserId());
        int newBlogCount = temp.getBlogCount() + 1;
        userDao.updateBlogCount(user.getUserId(), newBlogCount);
        //5.跳转到博客页
        resp.sendRedirect("blog_list.html");

        //输出日志
        Log log = new Log();
        log.setUserId(user.getUserId());
        log.setAction("该用户发表博客成功！");
        LogDao logDao = new LogDao();
        logDao.insert(log);
    }
}
